<template>
  <!--<div class="container loginIn" style="backgroundImage: url(http://codegen.caihongy.cn/20201125/f8ad26215c3a4476a1022930617e90f1.jpg)">-->
  <div class="container loginIn" :style="{ backgroundImage: `url(${require('@/assets/img/login3.jpg')})` }">
    <div class="image-overlay" style="position: absolute; top: 50px; left: 75px;">
      <img src="@/assets/img/jidalogose.png" alt="Your Image" style="width: 70%; height: 70%; object-fit: contain;">
    </div>

    <div :class="2 == 1 ? 'login-box left' : 2 == 0 ? 'login-box center' : 'login-box right'">

      <div class="hoverable" v-if="showTable" @click="adminLogin()">
        <i class="el-icon-user" style="font-size: 25px; color: #000000;padding-left: 7px"></i>
      </div>

      <div class="hoverable" v-if="!showTable" @click="otherLogin()">
        <i class="el-icon-user" style="font-size: 25px; color: white; padding-top: 3px;padding-left: 7px"></i>
      </div>

      <el-form class="login-form" label-position="left" :label-width="1 == 3 ? '56px' : '0px'">
        <el-form-item :label="1 == 3 ? '用户名' : ''" :class="'style'+1">
          <span v-if="1 != 3" class="svg-container" style="color:rgb(96,96,96);line-height:25px">
            <svg-icon icon-class="user"/>
          </span>
          <el-input placeholder="请输入账号" name="username" type="text" v-model="rulesForm.username"/>
        </el-form-item>

        <el-form-item :label="1 == 3 ? '密码' : ''" style="margin-bottom: 0">
          <span v-if="1 != 3" class="svg-container" style="color:rgb(96,96,96);line-height:25px">
            <svg-icon icon-class="password"/>
          </span>
          <el-input placeholder="请输入密码" name="password" type="password" v-model="rulesForm.password"/>
        </el-form-item>

        <el-form-item label="身份" prop="loginInRole" class="role">
          <div v-if="!showTable">
            管理员
          </div>
          <el-radio
              v-for="item in menus"
              v-if="showTable"
              v-bind:key="item.tableName"
              v-model="rulesForm.role"
              :label="item.tableName"
          >
            {{ item.role }}
          </el-radio>
        </el-form-item>
        <el-button type="primary" @click="login()" class="loginInBt"
                   style="padding:0;font-size:18px;border-radius:5px;height:40px;line-height:40px;width:100%;
                   backgroundColor:rgb(18,105,190); borderColor:rgb(43,59,158); color:rgb(255,255,255)">
          {{ '登录' }}
        </el-button>

        <div style="font-size: 13px;font-weight: 400; margin-top: 7%">
          温馨提示：学生账号为学号，教职工账号为12位工号或原应用集成平台中使用的邮箱账号（“@”之前的部分），
          默认密码与账号相同。管理员请通过右上角登录
        </div>
      </el-form>
    </div>

  </div>
</template>
<script>
import menu from "@/utils/menu";
import show from "@/views/modules/Mymentor/show.vue";

export default {
  computed: {
    show() {
      return show
    }
  },
  data() {
    return {
      rulesForm: {
        username: "",
        password: "",
        role: "",
        code: '',
      },

      showTable: true,

      menus: [{
        role: '学生',
        tableName: "student"
      }, {
        role: '导师',
        tableName: "mentor"
      }],

      tableName: "",

      codes: [{
        num: 1,
        color: '#000',
        rotate: '10deg',
        size: '16px'
      }, {
        num: 2,
        color: '#000',
        rotate: '10deg',
        size: '16px'
      }, {
        num: 3,
        color: '#000',
        rotate: '10deg',
        size: '16px'
      }, {
        num: 4,
        color: '#000',
        rotate: '10deg',
        size: '16px'
      }],
    };
  },
  mounted() {
  },
  created() {
    this.setInputColor()
    this.getRandCode()
  },
  methods: {
    setInputColor() {
      this.$nextTick(() => {
        document.querySelectorAll('.loginIn .el-input__inner').forEach(el => {
          el.style.backgroundColor = "rgba(211, 204, 204, 0.26)"
          el.style.color = "rgba(120, 118, 118, 1)"
          el.style.height = "44px"
          el.style.lineHeight = "44px"
          el.style.borderRadius = "20px"
        })
        document.querySelectorAll('.loginIn .style3 .el-form-item__label').forEach(el => {
          el.style.height = "44px"
          el.style.lineHeight = "44px"
        })
        document.querySelectorAll('.loginIn .el-form-item__label').forEach(el => {
          el.style.color = "rgba(183, 131, 59, 1)"
        })
        setTimeout(() => {
          document.querySelectorAll('.loginIn .role .el-radio__label').forEach(el => {
            el.style.color = "rgba(183, 131, 59, 1)"
          })
        }, 350)
      })

    },

    adminLogin() {
      this.rulesForm.role = "admin"
      this.tableName = "admin"
      this.showTable = false
    },

    otherLogin() {
      this.tableName = ""
      this.showTable = true
    },

    // 登陆
    async login() {
      let code = ''
      for (let i in this.codes) {
        code += this.codes[i].num
      }
      //目前不使用验证码,写死判断
      if ('0' == '1' && !this.rulesForm.code) {
        this.$message.error("请输入验证码");
        return;
      }
      if ('0' == '1' && this.rulesForm.code.toLowerCase() != code.toLowerCase()) {
        this.$message.error("验证码输入有误");
        this.getRandCode()
        return;
      }
      if (!this.rulesForm.username) {
        this.$message.error("请输入用户名");
        return;
      }
      if (!this.rulesForm.password) {
        this.$message.error("请输入密码");
        return;
      }
      if (!this.rulesForm.role) {
        this.$message.error("请选择角色");
        return;
      }

      let menus = this.menus;
      for (let i = 0; i < menus.length; i++) {
        if (menus[i].tableName == this.rulesForm.role) {
          this.tableName = menus[i].tableName;
        }
      }
      await this.$http({
        url: `${this.tableName}/login`, // 只需设置 API 的基础 URL
        method: "post",
        headers: {
          'Content-Type': 'application/json' // 确保请求头设置为 JSON
        },
        data: this.tableName === "admin"
            ? {
              [`${this.tableName}name`]: this.rulesForm.username, // 使用计算属性名称
              [`${this.tableName}password`]: this.rulesForm.password
            }
            : {
              [`${this.tableName}account`]: this.rulesForm.username, // 使用计算属性名称
              [`${this.tableName}password`]: this.rulesForm.password
            }
      }).then(({data}) => {
        this.$storage.set("Token", data.data.token);
        this.$http({
          url: `${this.tableName}/get-user`,
          method: 'get'
        }).then(({data}) => {
          if (data) {
            if (this.tableName === 'admin') {
              this.$storage.set("userName", data.data.adminname);
            } else if (this.tableName === 'student') {
              this.$storage.set("userName", data.data.studentname);
            } else if (this.tableName === 'mentor') {
              this.$storage.set("userName", data.data.mentorname);
            }
          }
          if (data) {
            this.$router.replace({path: "/index/"});
          } else {
            this.$message.error('用户名或密码不正确');
          }
        })
      }).catch(error => {
        this.$message.error("用户名或密码不正确");
      });
    },
    getRandCode(len = 4) {
      this.randomString(len)
    },
    randomString(len = 4) {
      let chars = [
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
        "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
        "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
        "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
        "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
        "3", "4", "5", "6", "7", "8", "9"
      ]
      let colors = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"]
      let sizes = ['14', '15', '16', '17', '18']

      let output = [];
      for (let i = 0; i < len; i++) {
        // 随机验证码
        let key = Math.floor(Math.random() * chars.length)
        this.codes[i].num = chars[key]
        // 随机验证码颜色
        let code = '#'
        for (let j = 0; j < 6; j++) {
          let key = Math.floor(Math.random() * colors.length)
          code += colors[key]
        }
        this.codes[i].color = code
        // 随机验证码方向
        let rotate = Math.floor(Math.random() * 60)
        let plus = Math.floor(Math.random() * 2)
        if (plus == 1) rotate = '-' + rotate
        this.codes[i].rotate = 'rotate(' + rotate + 'deg)'
        // 随机验证码字体大小
        let size = Math.floor(Math.random() * sizes.length)
        this.codes[i].size = sizes[size] + 'px'
      }
    },
  }
};
</script>
<style lang="scss" scoped>

.loginIn {
  min-height: 100vh;
  position: relative;
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;

  .login-box {
    .login-form {
      background-color: transparent;
      width: 100%;
      height: 65%;
      //right: inherit;
      top: 5%;
      position: relative;
      padding: 0 12px;
      box-sizing: border-box;
      display: flex;
      justify-content: center;
      flex-direction: column;
    }


    .title-container {
      text-align: center;
      font-size: 24px;
      font-family: 微軟正黑體;

      .title {
        margin: 20px;
      }
    }

    .el-form-item {
      position: relative;

      .svg-container {
        padding: 6px 5px 6px 15px;
        color: #f1e6e6;
        vertical-align: middle;
        display: inline-block;
        position: absolute;
        left: 0;
        top: 0;
        z-index: 1;
        line-height: 40px;
        width: 30px;
        text-align: center;
      }

      .el-input {
        display: inline-block;
        height: 40px;
        width: 100%;
        background-color: #eeeeee;
        border-radius: 5px; // 边角-圆角半径
        & /deep/ .el-input__inner {
          background-color: transparent !important;
          border: 1px solid #525252;
          border-radius: 5px !important;
          height: 40px !important;
          text-align: center;
          box-sizing: border-box;
        }

        & /deep/ .el-input__inner::placeholder {
          color: #656565; /* 更深的颜色 */
          opacity: 1; /* 确保颜色不被透明度影响 */
        }
      }
    }
  }

  .left {
    position: absolute;
    left: 0;
    top: 0;
    width: 360px;
    height: 100%;
  }

  .center {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 360px;
    transform: translate3d(-50%, -50%, 0);
    height: 446px;
    //border-radius: 8px;
  }

  .right {
    position: absolute;
    left: inherit;
    right: 0;
    top: 20%;
    width: 360px;
    height: 420px;
    margin-right: 8%;
    background-color: rgba(238, 238, 238, 0.81);
    background: linear-gradient(rgb(255, 255, 255), rgba(124, 104, 104, 0.25));
    backdrop-filter: blur(3px); /* 添加模糊效果 */
    -webkit-backdrop-filter: blur(5px); /* 兼容 Safari */
    border: #dadada solid 1px;
  }

  .code {
    .el-form-item__content {
      position: relative;

      .getCodeBt {
        position: absolute;
        right: 0;
        top: 0;
        line-height: 40px;
        width: 100px;
        background-color: rgba(51, 51, 51, 0.4);
        color: #fff;
        text-align: center;
        border-radius: 0 4px 4px 0;
        height: 40px;
        overflow: hidden;

        span {
          padding: 0 5px;
          display: inline-block;
          font-size: 16px;
          font-weight: 600;
        }
      }

      .el-input {
        & /deep/ input {
          padding: 0 130px 0 30px;
        }
      }
    }
  }

  .setting {
    & /deep/ .el-form-item__content {
      padding: 0 15px !important;
      box-sizing: border-box;
      line-height: 32px;
      height: 32px;
      font-size: 14px;
      color: #999;
      margin: 0 !important;

      .register {
        float: left;
        width: 50%;
      }

      .reset {
        float: right;
        width: 50%;
        text-align: right;
      }
    }
  }

  .style2 {
    padding-left: 30px;

    .svg-container {
      left: -30px !important;
    }

    .el-input {
      & /deep/ input {
        padding: 0 15px !important;
      }
    }
  }

  .code.style2, .code.style3 {
    .el-input {
      & /deep/ input {
        padding: 0 115px 0 15px;
      }
    }
  }

  .style3 {
    & /deep/ .el-form-item__label {
      padding-right: 6px;
    }

    .el-input {
      & /deep/ input {
        padding: 0 15px !important;
      }
    }
  }

  .role {
    & /deep/ .el-form-item__label {
      width: 20% !important;
      color: #3f3f3f !important;
    }

    & /deep/ .el-radio {
      margin-right: 12px;
    }

    & /deep/ .el-radio__label {
      color: #3f3f3f !important;
    }
  }

  .hoverable {
    height: 8%;
    width: 12%;
    cursor: pointer;
    background-color: rgba(185, 182, 182, 0.29);
    margin-left: auto;
    display: flex;
    justify-content: center;
    transition: background-color 0.3s; /* 添加过渡效果 */
    border-bottom-left-radius: 25px; /* 四分之一圆角 */
    align-items: center; /* 垂直居中 */
    box-shadow: 0 1px 5px rgba(0, 0, 0, 0.47); /* 可选：添加阴影效果 */
  }

  .hoverable:hover {
    background-color: rgba(100, 100, 100, 0.7); /* 鼠标悬停时的背景颜色 */
  }

  & /deep/ .el-form-item--medium {
    /* 调整高度、边距、字体大小等 */
    margin-bottom: 15px; /* 调整底部边距 */
  }
}
</style>
